/*
 * @Author: silence silence9685@163.com
 * @Date: 2023-04-21 11:32:48
 * @LastEditors: silence silence9685@163.com
 * @LastEditTime: 2023-07-19 14:01:22
 * @FilePath: \lenercom_bluetooth_app\lib\pages\Mine\AccountManagerPage.dart
 * @Description:选择国家页
 */
import 'package:ems_neutral_app/common/widgets/cubitWidget/DividingLine.dart';
import 'package:ems_neutral_app/pages/Mine/Language/SilLocalizations/SilLocalizations.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sil_base_widget_package/sil_base_widget_package.dart';

import '../../../api/R.dart';
import '../../../base/SilScaffold.dart';
import '../../../common/interfaceInstance/FutureBuilderInterfaceInstance.dart';
import '../../../common/widgets/buttons/NavSearchBtn.dart';
import '../../../entities/CountryEntity.dart';
import '../../../utils/color/ColorUtils.dart';
import '../../../utils/router/ARouter.dart';
import '../Language/LanguageController.dart';
import 'Controller/AccountManagerController.dart';

class ChooseCountryPage extends StatefulWidget {
  const ChooseCountryPage({Key? key}) : super(key: key);

  @override
  State<ChooseCountryPage> createState() => _ChooseCountryPageState();
}

class _ChooseCountryPageState extends State<ChooseCountryPage>
    with FutureBuilderInterfaceInstance {
  AccountManagerController _accountManagerController =
      Get.put(AccountManagerController());

  List<CountryEntity> _list = [];

  dynamic _future;
  String? _searchValue;
  @override
  void initState() {
    super.initState();
    _future = searchHandler();
  }

  searchHandler() async {
    R r = await _accountManagerController.requestSearchCountry(
        searchContext: _searchValue);
    _list = r.data;
    return r.data;
  }

  @override
  Widget build(BuildContext context) {
    return SilScaffold(
        title: LangOf(context)?.sel_country?.search,
        body: SafeArea(child: mainWidget()));
  }

  mainWidget() {
    return Column(
      children: [
        Padding(padding: EdgeInsets.only(top: 20.w)),
        NavSearchBtn(onChange: (valid, value) {
          _searchValue = value;
          setState(() {
            _future = searchHandler();
          });
        }),
        Expanded(
            child: SilFutureBuilderWidget(
                future: _future,
                mainWidget: (data) {
                  return contentListView();
                },
                reloadFunc: () {
                  setState(() {
                    _future = searchHandler();
                  });
                },
                interface: this))
      ],
    );
  }

  contentListView() {
    LanguageController languageController = LanguageController();
    bool isPromptEnglish = languageController.isPromptEnglish();
    return ListView.separated(
        padding: EdgeInsets.only(top: 16.w, bottom: 20.w),
        itemBuilder: ((context, index) {
          CountryEntity entity = _list[index];
          return contentListViewItem(entity,isPromptEnglish);
        }),
        separatorBuilder: (context, index) {
          return DividingLine();
        },
        itemCount: _list.length);
  }

  contentListViewItem(CountryEntity entity,bool isPromptEnglish) {
    return SilEmptyButton(
      margin: EdgeInsets.symmetric(vertical: 20.w),
      useConstrained: true,
      alignment: Alignment.centerLeft,
      onTapFunc: () {
        ARoute.pop(context, entity);
      },
      child: SilText(isPromptEnglish ? entity.enCountry : entity.cnCountry,
          fontSize: 14.sp,
          color: ColorUtils.color_font_main,
          fontWeight: FontWeight.w500,
          textAlign: TextAlign.left),
    );
  }
}
